"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.radioProps = exports.default = void 0;

var _vue = require("vue");

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));

var _vcCheckbox = _interopRequireDefault(require("../vc-checkbox"));

var _classNames2 = _interopRequireDefault(require("../_util/classNames"));

var _useConfigInject2 = _interopRequireDefault(require("../_util/hooks/useConfigInject"));

var _FormItemContext = require("../form/FormItemContext");

var __rest = void 0 && (void 0).__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

var radioProps = {
  prefixCls: _vueTypes.default.string,
  checked: _vueTypes.default.looseBool,
  disabled: _vueTypes.default.looseBool,
  isGroup: _vueTypes.default.looseBool,
  value: _vueTypes.default.any,
  name: _vueTypes.default.string,
  id: _vueTypes.default.string,
  autofocus: _vueTypes.default.looseBool,
  type: _vueTypes.default.string.def('radio'),
  onChange: _vueTypes.default.func,
  onFocus: _vueTypes.default.func,
  onBlur: _vueTypes.default.func
};
exports.radioProps = radioProps;

var _default = (0, _vue.defineComponent)({
  name: 'SRadio',
  props: radioProps,
  emits: ['update:checked', 'update:value', 'change', 'blur', 'focus'],
  setup: function setup(props, _ref) {
    var emit = _ref.emit,
        expose = _ref.expose,
        slots = _ref.slots;
    var formItemContext = (0, _FormItemContext.useInjectFormItemContext)();
    var vcCheckbox = (0, _vue.ref)();
    var radioGroupContext = (0, _vue.inject)('radioGroupContext', undefined);

    var _useConfigInject = (0, _useConfigInject2.default)('radio', props),
        prefixCls = _useConfigInject.prefixCls,
        direction = _useConfigInject.direction;

    var focus = function focus() {
      vcCheckbox.value.focus();
    };

    var blur = function blur() {
      vcCheckbox.value.blur();
    };

    expose({
      focus: focus,
      blur: blur
    });

    var handleChange = function handleChange(event) {
      var targetChecked = event.target.checked;
      emit('update:checked', targetChecked);
      emit('update:value', targetChecked);
      emit('change', event);
      formItemContext.onFieldChange();
    };

    var onChange = function onChange(e) {
      emit('change', e);

      if (radioGroupContext && radioGroupContext.onRadioChange) {
        radioGroupContext.onRadioChange(e);
      }
    };

    return function () {
      var _classNames;

      var radioGroup = radioGroupContext;

      var customizePrefixCls = props.prefixCls,
          _props$id = props.id,
          id = _props$id === void 0 ? formItemContext.id.value : _props$id,
          restProps = __rest(props, ["prefixCls", "id"]);

      var rProps = (0, _extends2.default)({
        prefixCls: prefixCls.value,
        id: id
      }, restProps);

      if (radioGroup) {
        rProps.name = radioGroup.props.name;
        rProps.onChange = onChange;
        rProps.checked = props.value === radioGroup.stateValue.value;
        rProps.disabled = props.disabled || radioGroup.props.disabled;
      } else {
        rProps.onChange = handleChange;
      }

      var wrapperClassString = (0, _classNames2.default)((_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(prefixCls.value, "-wrapper"), true), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls.value, "-wrapper-checked"), rProps.checked), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls.value, "-wrapper-disabled"), rProps.disabled), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls.value, "-wrapper-rtl"), direction.value === 'rtl'), _classNames));
      return (0, _vue.createVNode)("label", {
        "class": wrapperClassString
      }, [(0, _vue.createVNode)(_vcCheckbox.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, rProps), {}, {
        "ref": vcCheckbox
      }), null), slots.default && (0, _vue.createVNode)("span", null, [slots.default()])]);
    };
  }
});

exports.default = _default;